home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 457 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.0 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: maney@mcs.com (Martin J. Maney)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Article for comp.std.c++: Eliminating #ifdef: if const
  5. Date: 23 Feb 1996 15:55:14 GMT
  6. Organization: MCSNet Services
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4gjerl$rg4@Venus.mcs.com>
  9. References: <199602160807.IAA06126@condor.ukc.ac.uk> <4gb0a4$sa3@fido.asd.sgi.com> <4giddp$7q6@mulga.cs.mu.OZ.AU>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. X-Newsreader: TIN [version 1.2 PL2 (KSD)]
  12. Originator: clamage@taumet
  13.  
  14. Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
  15. > shankar@mti.mti.sgi.com (Shankar Unni) writes:
  16.  
  17. > >Often, an #ifdef covers just a small part of an expression that may be
  18. > >different between different environments, or some such small differences:
  19. > >
  20. > >   if (some condition) {
  21. > >#ifdef SOMEIMPL
  22. > >    if (some other condition) {
  23. > >#endif /* SOMEIMPL */
  24. > >    /* code */
  25. > >    /* code */
  26. > >    /* code */
  27. > >#ifdef SOMEIMPL
  28. > >    }
  29. > >#endif /* SOMEIMPL */
  30. > >   }
  31.  
  32. > But with `if const', your 11 lines of code could be rewritten as either
  33.  
  34. >     if (some condition) {
  35. >         if (SOMEIMPL && some other condition) {
  36. >             /* code */
  37. >             /* code */
  38. >             /* code */
  39. >         }
  40. >     }
  41.  
  42. >     (7 lines of code)
  43.  
  44. Of course it's a little unfair to pick on the specific features of the 
  45. example, but I can no longer resist the temptation to point out that all 
  46. of these are simply awful ways to deal with this sort of target 
  47. dependency.  What you truly want in these cases is to replace the 
  48. platform-dependent hackery with
  49.  
  50.   if (implementation_defined_test())
  51.   {
  52.       ...
  53.   }
  54.  
  55. And let implementation_defined_test() be defined in whatever place you 
  56. like to collect portability [sic] stuff.  In C one might have to choose 
  57. between a function-like macro that could be unsafe and a function that 
  58. might be too costly, but in C++ you can have hte best of both worlds if 
  59. you make it an inline function.
  60.  
  61. Again: I realize there are other applications for the proposed idea; I 
  62. encourage its supporters to present them and make a better case for this 
  63. feature.  But this example strikes me as an argument _against_ the 
  64. feature on the grounds that it encourages what I am happy to call a 
  65. simply awful coding style.
  66.  
  67. > Both of these solutions have the advantage that the test for `SOMEIMPL'
  68. > occurs only once, so things will be easier if you later have to change it to 
  69. > `SOMEIMPL || SOMEOTHERIMPL'.
  70.  
  71. Ditto.  Plus the advantage that if this condition is tested for elsewhere 
  72. - and if it isn't now, what are the odds it will be before the program is 
  73. finally retired from service? - the inline function, like the ugly C 
  74. macro, will at least have a chance of getting the change into every piece 
  75. of code that uses it, if only the dependencies are setup correctly.  ;-)
  76.  
  77. > >Duplicating all the code so that each half is precisely correct in its {}
  78. > >matching is both tedious and error-prone (trying to verify that both parts
  79. > >of the expression are fixed).
  80.  
  81. > But fortunately as shown above it would not be required.
  82.  
  83. Ditto.
  84.  
  85. > >Also, the #ifdefs serve a purpose in pointing out the fact that this piece
  86. > >of code is system-dependent.  The syntax you propose hides its warts rather
  87. > >too well, and make it easy for a reader to miss the significant piece of
  88. > >information that this area of code is system-dependent.
  89.  
  90. > True, but a convention of using upper-case for SOMEIMPL and similar constants
  91. > would make such tests stand out.
  92.  
  93. I will concede that the inline function hides this as well as you want to
  94. - I used a long, blatant name in my example for precisely this reason. 
  95. But then again, if you don't have to worry about writing the details of
  96. the target-dependent code all over the place, what difference does it make
  97. how visible it is or isn't in use? 
  98.  
  99. [ To submit articles: Try just posting with your newsreader.
  100.               If that fails, use mailto:std-c++@ncar.ucar.edu
  101.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  102.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  103.   Comments? mailto:std-c++-request@ncar.ucar.edu
  104. ]
  105.